<?php

namespace Home\Controller;

/**
 * Description of BookPageController.class控制器实例
 *
 * @author songanwei
 * @date 2016-1-9
 */
class BookPageController extends ParentController {

    /**
     * @description 房态页面视图
     * @modify 2016-01-09
     * @modify trainyao 2016-06-28 增加房间权限判断
     * @modify zhangzhen 2017-05-26 返回钟点房
     * @modify zhangzhen 2017-06-16 返回长租房
     */
    public function index() {
        $hotelId = $this->hotelId;
        if (!empty($hotelId)) {
            // 房型和房间号
            $roomTypeLogic = A('Common/RoomType', 'Logic');
            $roomInfo = $roomTypeLogic->getInfoByHotel($hotelId);
            $roomTypeIds = array_keys($roomInfo);
            // 维修房数据
            $roomServiceModel = D('RoomService');
            $roomServiceList = $roomServiceModel->getServiceList($roomTypeIds, null, null, NOT_DELETE, true);
            $roomServiceList = getArrayValueAsIndex($roomServiceList, 'roominfo_id');

            // 判断是否需要根据房间权限筛选房间信息
            $adminId = getUserId();
            $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
            if (!$adminAuthorityLogic->checkIsAdmin($adminId, $hotelId)) {
                $accountLogic = A('Common/Account', 'Logic');
                $roomAuthority = $accountLogic->getAdminRoomAuthority($adminId, $hotelId);
                foreach ($roomInfo as $index => &$roomTypeItem) {
                    foreach ($roomTypeItem['room'] as $roomIndex => $roomItem) {
                        if (!in_array($roomItem['id'], $roomAuthority)) {
                            unset($roomTypeItem['room'][$roomIndex]);
                        }
                    }
                    unset($roomItem);

                    if (empty($roomTypeItem['room'])) {
                        unset($roomInfo[$index]);
                    } else {
                        $roomTypeItem['room'] = array_values($roomTypeItem['room']);
                    }
                }
                unset($roomTypeItem);
            }
            $roomsCount = 0;
            if (!empty($roomInfo)) {
                foreach ($roomInfo as &$roomType) {
                    if (!empty($roomType['room'])) {
                        $roomsCount += count($roomType['room']);
                        foreach ($roomType['room'] as $key => &$roomTypeItem) {
                            // 是否维修房
                            if (isset($roomServiceList[$roomTypeItem['id']])) {
                                $roomTypeItem['isService'] = $roomServiceList[$roomTypeItem['id']]['isservice'];
                            } else {
                                $roomTypeItem['isService'] = ROOM_CLEAN;
                            }
                        }
                        unset($roomTypeItem);
                    }
                }
            }
            $hotelLogic = A('Common/Hotel', 'Logic');
            //检查是否已到期
            $result = $hotelLogic->checkHotelActive($hotelId);
            if ($result == false) {
                $this->messageTips(L('_HOTEL_EXPIRED_'));
            }

            //返回声音开关
            $hotelEntityModel = D('HotelEntity');
            $hotelEntityInfo = $hotelEntityModel->field('voicestatus,clientswitch')->where(['id'=>$hotelId])->find();
            $this->assign('voicestatus', $hotelEntityInfo['voicestatus']);
            $this->assign('isClientAvailable', $hotelEntityInfo['clientswitch']);
            $this->assign('userId', $adminId);
            // 没有房态查看权限
            if (!checkAccess($this->adminAuthority, 'Home/BookPage/index')) {
                $this->display();
                exit();
            }

            $this->assign('roomsCount', $roomsCount);
            $this->assign('rooms', $roomInfo);
            //钟点房
            $hourRooms =D('HourRoom')->getInfosJoinName($hotelId);
            $this->assign('hourRooms', $hourRooms);

            //长租房
            $longRooms =D('LongRoom')->getInfosJoinName($hotelId);
            $this->assign('longRooms', $longRooms);

            $hotelPayData = $hotelLogic->getHotelPay($hotelId, false, null, true);
            $this->assign('hotelpay', $hotelPayData);
            $noCardhotelPayData = $hotelLogic->getHotelPay($hotelId, false);
            $this->assign('noCardhotelPayData', $noCardhotelPayData);

            // 记一笔项目数据
            $hotelNoteData = $hotelLogic->getHotelNote($hotelId, false, null, true);
            $this->assign('noteInCome', $hotelNoteData['income']);
            $this->assign('noteOutCome', $hotelNoteData['outcome']);

            // 其他消费项目数据
            $otherCostData = $hotelLogic->getHotelOtherCost($hotelId);
            $this->assign('otherCost', $otherCostData);

            $hotelChannelData = $hotelLogic->getHotelChannel($hotelId, false);
            $this->assign('hotelchannel', $hotelChannelData);

            // 获取系统货币
            $currencyLogic = A('Common/Currency', 'Logic');
            $currencyData = $currencyLogic->getBpCurrency($hotelId);
            $currencyDefaultData = my_sort($currencyData, 'type', SORT_DESC)[0];
            $this->assign('currencyInfo', $currencyData);
            $this->assign('currencyDefaultInfo', $currencyDefaultData);
        }
        $param = I('get.');
        $validateRule = array(
            'viewstyle' => 'integer'
        );
        $this->ajaxValidateReturn(new \Org\Util\Validate(), $param, $validateRule);
        $roomViewStyle = $param['viewstyle'];
        if (empty($roomViewStyle)) {
            $roomViewStyle = cookie(C('COOKIE_ROOM_VIEW_STYLE'));
        }
        $isVerticleStyle = $roomViewStyle == C('COOKIE_ROOM_VIEW_STYLE_VERTICLE');
        $this->assign('startDay', strtotime('-3 days'));
        $this->assign('endDay', strtotime('+27 days'));

        $hourRoomInitModel = D('HourRoomInit');
        $hourRoomInit = $hourRoomInitModel->getInfo($hotelId);

        $this->assign('hourRoom', $hourRoomInit);

        if ($isVerticleStyle) {
            cookie(C('COOKIE_ROOM_VIEW_STYLE'), C('COOKIE_ROOM_VIEW_STYLE_VERTICLE'));
            $this->display('reverse');
        } else {
            cookie(C('COOKIE_ROOM_VIEW_STYLE'), C('COOKIE_ROOM_VIEW_STYLE_DEFAULT'));
            $this->display();
        }
    }

}
